package com.luo.chengrui.module.sa.controller.admin.role;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import com.luo.chengrui.module.sa.controller.admin.role.vo.*;
import com.luo.chengrui.module.sa.convert.role.RoleFolderUserConvert;
import com.luo.chengrui.module.sa.dal.dataobject.role.RoleFolderUserDO;
import com.luo.chengrui.module.sa.dal.dataobject.role.RoleFolderUserViewDO;
import com.luo.chengrui.module.sa.service.role.RoleFolderUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 角色分类人员配置表")
@RestController
@RequestMapping("/sa/role-folder-user")
@Validated
public class RoleFolderUserController {

    @Resource
    private RoleFolderUserService roleFolderUserService;

    @PostMapping("/create")
    @Operation(summary = "创建角色分类人员配置表")
    @PreAuthorize("@ss.hasPermission('sa:role-folder-user:config')")
    public CommonResult<String> createRoleFolderUser(@Valid @RequestBody RoleFolderUserCreateReqVO createReqVO) {
        return success(roleFolderUserService.createRoleFolderUser(createReqVO));
    }

    @PostMapping("/batchcreate")
    @Operation(summary = "创建组织机构管理")
    @PreAuthorize("@ss.hasPermission('sa:role-folder-user:config')")
    public CommonResult<Boolean> batchCreateOrg(@Valid @RequestBody List<RoleFolderUserCreateReqVO> createReqVO) {
        roleFolderUserService.batchCreateOrg(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色分类人员配置表")
    @PreAuthorize("@ss.hasPermission('sa:role-folder-user:config')")
    public CommonResult<Boolean> updateRoleFolderUser(@Valid @RequestBody RoleFolderUserUpdateReqVO updateReqVO) {
        roleFolderUserService.updateRoleFolderUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色分类人员配置表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:role-folder-user:delete')")
    public CommonResult<Boolean> deleteRoleFolderUser(@RequestParam("id") String id) {
        roleFolderUserService.deleteRoleFolderUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色分类人员配置表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:role-folder-user:config')")
    public CommonResult<RoleFolderUserRespVO> getRoleFolderUser(@RequestParam("id") String id) {
        RoleFolderUserDO roleFolderUser = roleFolderUserService.getRoleFolderUser(id);
        return success(RoleFolderUserConvert.INSTANCE.convert(roleFolderUser));
    }

    @GetMapping("/list")
    @Operation(summary = "获得角色分类人员配置表列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:role-folder-user:config')")
    public CommonResult<List<RoleFolderUserRespVO>> getRoleFolderUserList(@RequestParam("ids") Collection<String> ids) {
        List<RoleFolderUserDO> list = roleFolderUserService.getRoleFolderUserList(ids);
        return success(RoleFolderUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/person")
    @Operation(summary = "根据人员岗位ID，查询人员管理的角色分类列表")
    @Parameter(name = "authType", description = "权限类型", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('sa:role-folder-user:config')")
    public CommonResult<List<String>> getMyRoleFolderList(@RequestParam(value = "authType", required = false) String authType) {
        List<String> list = roleFolderUserService.getCurrentUserRoleFolderList(authType);
        return success(list);
    }

    @GetMapping("/page")
    @Operation(summary = "获得角色分类人员配置表分页")
    @PreAuthorize("@ss.hasPermission('sa:role-folder-user:config')")
    public CommonResult<PageResult<RoleFolderUserRespVO>> getRoleFolderUserPage(@Valid RoleFolderUserPageReqVO pageVO) {
        PageResult<RoleFolderUserViewDO> pageResult = roleFolderUserService.getRoleFolderUserPageV2(pageVO);
        return success(RoleFolderUserConvert.INSTANCE.convertPageCustomize(pageResult));
    }

}
